package org.example.spring.controller.Admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.spring.domain.pojo.Category;
import org.example.spring.domain.pojo.Result;
import org.example.spring.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Set;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/getAllCategory")
    @ApiOperation(value = "获取所有分类")
    public Result<ArrayList<Category>> getAllCategory() {

        //查询缓存
        String pattern = "category_*";
        Set<String> keys = redisTemplate.keys(pattern); // 获取所有以 category_ 开头的键
        ArrayList<Category> categories = new ArrayList<>();
        if (keys.size() > 0) {
            for (String key : keys) {
                Category category = (Category) redisTemplate.opsForValue().get(key); // 获取值并转换为 Category 对象
                if (category != null) {
                    categories.add(category); // 添加到数组
                }
            }
        }
        if (categories.size() > 0) {
            return Result.success(categories);
        }
        else{
            categories = categoryService.getAllCategory();
            for(Category category : categories) {
                String key= "category_" + category.getCategoryId();
                redisTemplate.opsForValue().set(key, category);
            }
        }
       return Result.success(categories);
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/deleteCategory")
    public Result deleteCategory(@RequestParam String categoryName) {
        boolean flag = categoryService.deleteCategory(categoryName);
        if(flag) {
            cleanCache("category_*");
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败，分类下有商品");
        }
    }
    
    @ApiOperation("更新分类")
    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category) {
        boolean flag= categoryService.updateCategory(category);
        if(!flag) {
            cleanCache("category_*");
            return Result.error("更新失败，分类已存在");
        }
         return Result.success("修改成功");
    }

    @ApiOperation("添加分类")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestParam String categoryName) {
        boolean flag= categoryService.addCategory(categoryName);
        if(!flag) {
            cleanCache("category_*");
            return Result.error("添加失败，分类已存在");
        }
         return Result.success("添加成功");
    }

    private void cleanCache(String patten){
        Set keys = redisTemplate.keys(patten);
        redisTemplate.delete(keys);
    }
}
